import com.powerfl.frm.taf.*;

import flash.utils.Dictionary;

private var _orders:Vector.<TAForder>;
private var _orderCursor:int;
private var _orderCurr:TAForder;
private var _orderIsPlaying:int;
private var _orderSub:Dictionary;
private var _orderBranchs:Vector.<Function>;
private var _orderCurrBranch:Function;

private function orderInit():void{
	_orders = new Vector.<TAForder>();
	_orderCursor = 0;
	_orderSub = new Dictionary( true );
	_orderBranchs = new Vector.<Function>();
}
//----------------------------------------------------------------------------------------------------------------------
final public function orderAdd( $tafParam:TAFparam, $order:Function, ...$param ):void{
	SELF.orderPush( function():void{
		$order.apply( null, $param );
		orderCollect( arguments.callee );
		if( $tafParam ){
			poolCollectParam( $tafParam );
		}
	} );
}
final public function orderAddByKeyCheck( $key:String, $tafParam:TAFparam, $order:Function, ...$param ):void{
	SELF.orderPush( function():void{
		if( treeCheck( $key ) || $key === '' ){
			$order.apply( null, $param );
			orderCollect( arguments.callee );
		}
		if( $tafParam ){
			poolCollectParam( $tafParam );
		}
	} );
}
//----------------------------------------------------------------------------------------------------------------------
final public function orderBranchAdd( $order:Function, ...$param ):void{
	SELF.orderPush( function():void{
		var callee:Function;
		SELF._orderCurrBranch = arguments.callee;
		SELF._orderBranchs[SELF._orderBranchs.length] = SELF._orderCurrBranch;
		$order.apply( null, $param );
		orderCollect( SELF._orderCurrBranch );
	} );
}
private function orderPush( $order:Function, ...$param ):void{
	var order:TAForder, j:int;
	order = poolPullOrder().init( $order, $param );
	if( SELF._orderBranchs.length ){
		if( SELF._orderSub[SELF._orderCurrBranch] ){}else{
			SELF._orderSub[SELF._orderCurrBranch] = poolPullVectorOrder();
		}
		j = SELF._orderSub[SELF._orderCurrBranch].length;
		SELF._orderSub[SELF._orderCurrBranch][j] = order;
	}else{
		SELF._orders[SELF._orders.length] = order;
		if( SELF._orderCurr === null ){
			SELF._orderCurr = order;
		}
		if( 1 < SELF._orders.length ){
			orderCheck();
		}else{
			order.run();
		}
	}
}
private function orderCollect( $order:Function ):void{
	var order:Function, i:int, j:int, sub:Vector.<TAForder>;
	order = $order;
	j = SELF._orderBranchs.length;
	if( j ){
		for( i = 0 ; i < j ; ++i ){
			sub = SELF._orderSub[SELF._orderCurrBranch];
			if( sub ){
				if( sub.length ){
					sub.shift().run();
					return;
				}else{
					poolCollectVectorOrder( SELF._orderSub[SELF._orderCurrBranch] );
					SELF._orderSub[SELF._orderCurrBranch] = null;
				}
			}
			order = SELF._orderCurrBranch;
			SELF._orderCurrBranch = null;
			SELF._orderBranchs.pop();
		}
	}
	if( order === SELF._orderCurr.order ){
		SELF._orderCurr.complete();
		orderCheck();
	}
	
}
private function orderCheck():void{
	if( SELF._orderCurr.isComplete < 0 ){
		poolCollectOrder( SELF._orderCurr );
		if( SELF._orderCursor < SELF._orders.length - 1 ){
			SELF._orderCurr = SELF._orders[++SELF._orderCursor];
			SELF._orderCurr.run();
		}else{
			SELF._orderCurr = null;
			SELF._orderCursor = 0;
			SELF._orders.length = 0;
		}
	}
}

